package com.muli.muwai.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.IntfCostRevenue;
import com.muli.muwai.mapper.CostRevenueMapper;
import com.muli.muwai.service.CostRevenueService;
import com.muli.muwai.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class CostRevenueServiceImpl extends ServiceImpl<CostRevenueMapper, IntfCostRevenue> implements CostRevenueService {
    @Autowired
    CostRevenueMapper costRevenueMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void costRevenue(List<String[]> costRevenue) throws Exception {

        List<IntfCostRevenue> intfCostRevenues = getIntfCostRevenues(costRevenue);
        HashSet<String> set = new HashSet<>();
        //获取月份
        for (IntfCostRevenue intfCostRevenue : intfCostRevenues) {
            String month = intfCostRevenue.getMonth();
            set.add(month);
        }
        if (set.size() == 1) {
            //删除同月份的数据
            String strip = StringUtils.strip(set.toString(), "[]");
            int aa = costRevenueMapper.deleteData(strip);
            saveBatch(intfCostRevenues);
            System.out.println(aa);
        } else {
            System.out.println(set.toString());
            throw new Exception("请检查所有月份为同一月或者同一格式后上传");
        }

        //测试
        //saveBatch(intfCostRevenues);
    }

    private List<IntfCostRevenue> getIntfCostRevenues(List<String[]> costRevenue) throws Exception {
        List<IntfCostRevenue> intfCostRevenues = new ArrayList<>();
        for (String[] strings : costRevenue) {
            IntfCostRevenue intfCostRevenue = new IntfCostRevenue();
            intfCostRevenue.setProfit(strings[1]);
            intfCostRevenue.setFiscalYear(strings[0]);
            intfCostRevenue.setRanking(strings[2]);
            intfCostRevenue.setMonth(strings[3]);
            intfCostRevenue.setShowRetail(strings[4]);
            intfCostRevenue.setOrderNumber(strings[5]);
            intfCostRevenue.setProjectName(strings[6]);
            // intfCostRevenue.setIncome(strings[7]);
            try {
                if (strings[7].equals("")) {
                    intfCostRevenue.setIncome(Double.valueOf(0));
                } else {
                    intfCostRevenue.setIncome(Double.parseDouble(strings[7]));
                }
            } catch (NumberFormatException e) {
                int size = intfCostRevenues.size() + 2;
                throw new Exception("第" + size + "行,第" + 8 + "列,格式错误");
            }

            //intfCostRevenue.setCost(strings[8]);
            try {
                if (strings[8].equals("")) {
                    intfCostRevenue.setCost(Double.valueOf(0));
                } else {
                    intfCostRevenue.setCost(Double.parseDouble(strings[8]));
                }
            } catch (NumberFormatException e) {
                int size = intfCostRevenues.size() + 2;
                throw new Exception("第" + size + "行,第" + 9 + "列,格式错误");
            }
            intfCostRevenue.setPartCompany(strings[9]);
            //Date date = DateUtils.parseString2Date(strings[10], "yy/MM/dd");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
                Date date1 = sdf.parse(strings[10]);
                Timestamp timestamp1 = new Timestamp(date1.getTime());
                intfCostRevenue.setDeliverTime(timestamp1);
            } catch (Exception e) {
                int size = intfCostRevenues.size() + 2;
                throw new Exception("第" + size + "行,第" + 11 + "列,请设写成yyyy/m/d格式");
            }
            intfCostRevenues.add(intfCostRevenue);
        }
        return intfCostRevenues;
    }

}
